package com.lv.service.utils

import android.content.Context
import com.blankj.utilcode.util.CollectionUtils

import com.lv.common.imp.OnPositionTListener
import com.lv.service.bean.PositionBean
import com.lv.service.bean.TelFourCategoryBean
import com.lv.service.bean.TelSecondCategoryBean
import com.lv.service.bean.TelThirdCategoryBean
import com.lv.service.room.address.ReceiptAddressItem
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation

/**
 * 作者： ygx
 * 创建日期：2023/2/11 2:49 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class YgxPickerUtilsKt {

    companion object {

        /**
         * 四级选择器数据初始化
         */
        fun initFourAddress(
            mCategoryBeans: MutableList<ReceiptAddressItem>,
            mProvince: String,
            mCity: String,
            mArea: String,
            mTown: String,
            default: Int = 0
        ): PositionBean {
            var mWheelPosition1 = default
            var mWheelPosition2 = default
            var mWheelPosition3 = default
            var mWheelPosition4 = default
            for (i in 0 until mCategoryBeans.size) {
                val name = mCategoryBeans[i].name ?: ""
                if (name.isNotEmpty() && (name.contains(mProvince) || mProvince.contains(name))) {
                    mWheelPosition1 = i
                    val telSecondCategories = mCategoryBeans[i].telSecondCategories
                    if (CollectionUtils.isNotEmpty(telSecondCategories))
                        for (j in 0 until telSecondCategories!!.size) {
                            val telsecondName = telSecondCategories[j].name ?: ""
                            if (telsecondName.isNotEmpty() && (telsecondName.contains(mCity) || mCity.contains(
                                    telsecondName
                                ))
                            ) {
                                mWheelPosition2 = j
                                val telThirdCategories =
                                    telSecondCategories[j].telThirdCategories
                                if (CollectionUtils.isNotEmpty(telThirdCategories)) {
                                    for (k in 0 until telThirdCategories!!.size) {
                                        val telThirdName = telThirdCategories[k].name ?: ""
                                        if (telThirdName.isNotEmpty() && (telThirdName.contains(
                                                mArea
                                            ) || mArea.contains(telThirdName))
                                        ) {
                                            mWheelPosition3 = k
                                            val telFourCategories =
                                                telThirdCategories[k].telFourCategories
                                            if (CollectionUtils.isNotEmpty(telFourCategories)) {
                                                for (m in 0 until telFourCategories!!.size) {
                                                    val telFourName =
                                                        telFourCategories[m].name ?: ""
                                                    if (telFourName.isNotEmpty() && (telFourName.contains(
                                                            mTown
                                                        ) || mTown.contains(telFourName))
                                                    ) {
                                                        mWheelPosition4 = m
                                                        break
                                                    }
                                                }

                                            }
                                            break
                                        }
                                    }

                                }
                                break
                            }
                        }

                    break
                }
            }
            return PositionBean(
                mWheelPosition1,
                mWheelPosition2,
                mWheelPosition3,
                mWheelPosition4
            )
        }

        /**
         * 为四级数据添加 全部
         */
        fun fourSelectedAddAll(datas: List<ReceiptAddressItem>): MutableList<ReceiptAddressItem> {
            val mAddressBeans = mutableListOf<ReceiptAddressItem>()
            for (element in datas) {
                val telSecondCategory = element.telSecondCategories
                val seconds = mutableListOf<TelSecondCategoryBean>()
                val telThirdCategories: MutableList<TelThirdCategoryBean> = ArrayList()
                telThirdCategories.add(
                    TelThirdCategoryBean(
                        "",
                        "全部",
                        "  ",
                        "",
                        "",
                        0,
                        false,
                        mutableListOf(TelFourCategoryBean("", "全部", "  "))
                    )
                )
                seconds.add(
                    TelSecondCategoryBean("0", "全部", "  ", "", "", 0, false, telThirdCategories)
                )
                telSecondCategory?.let {
                    for (i in telSecondCategory.indices) {
                        val telSecondCategoryBean = telSecondCategory[i]
                        val telThirdBeans = telSecondCategoryBean.telThirdCategories
                        val telThirds = mutableListOf<TelThirdCategoryBean>()
                        telThirds.add(
                            TelThirdCategoryBean(
                                "",
                                "全部",
                                "  ",
                                "",
                                "",
                                0,
                                false,
                                mutableListOf(TelFourCategoryBean("  ", "全部", "  "))
                            )
                        )
                        telThirdBeans?.let {
                            for (j in telThirdBeans.indices) {//循环三级，为每个四级加全部
                                val telThirdCategoryBean = telThirdBeans[j]
                                val telFourCategories = telThirdCategoryBean.telFourCategories
                                val telFourCas: MutableList<TelFourCategoryBean> = ArrayList()
                                telFourCas.add(
                                    TelFourCategoryBean(
                                        "",
                                        "全部",
                                        "  ",
                                        "",
                                        "",
                                        0,
                                        false
                                    )
                                )
                                telFourCategories?.let {
                                    telFourCas.addAll(telFourCategories)
                                }
                                telThirds.add(
                                    TelThirdCategoryBean(
                                        telThirdCategoryBean.id,
                                        telThirdCategoryBean.name,
                                        telThirdCategoryBean.firstChar,
                                        telThirdCategoryBean.forshort,
                                        telThirdCategoryBean.pforshort,
                                        telThirdCategoryBean.seq,
                                        telThirdCategoryBean.isShow,
                                        telFourCas
                                    )
                                )
                            }
                        }

                        telSecondCategoryBean.telThirdCategories = telThirds
                        seconds.add(telSecondCategoryBean)
                    }
                }
                val categoryBean = ReceiptAddressItem(
                    element._id,
                    element.id,
                    element.name,
                    element.firstChar,
                    element.forshort,
                    element.pforshort,
                    element.seq,
                    element.isShow,
                    seconds,
                )
                mAddressBeans.add(categoryBean)
            }
            return mAddressBeans
        }

        /**
         * 三级选择器数据初始化
         */
        fun initThreeAddress(
            mCategoryBeans: MutableList<ReceiptAddressItem>,
            mProvince: String,
            mCity: String,
            mArea: String,
        ): Triple<Int, Int, Int> {
            var mWheelPosition1 = 0
            var mWheelPosition2 = 0
            var mWheelPosition3 = 0
            for (i in 0 until mCategoryBeans.size) {
                val name = mCategoryBeans[i].name ?: ""
                if (name.contains(mProvince) || mProvince.contains(name)) {
                    mWheelPosition1 = i
                    val telSecondCategories = mCategoryBeans[i].telSecondCategories
                    if (CollectionUtils.isNotEmpty(telSecondCategories))
                        for (j in 0 until telSecondCategories!!.size) {
                            val telsecondName = telSecondCategories[j].name ?: ""
                            if (telsecondName.contains(mCity) || mCity.contains(telsecondName)) {
                                mWheelPosition2 = j
                                val telThirdCategories =
                                    telSecondCategories[j].telThirdCategories
                                if (CollectionUtils.isNotEmpty(telThirdCategories)) {
                                    for (k in 0 until telThirdCategories!!.size) {
                                        val telThirdName = telThirdCategories[k].name ?: ""
                                        if (telThirdName.contains(mArea) || mArea.contains(
                                                telThirdName
                                            )
                                        ) {
                                            mWheelPosition3 = k
                                            break
                                        }
                                    }

                                }
                                break
                            }
                        }

                    break
                }
            }
            return Triple(
                mWheelPosition1,
                mWheelPosition2,
                mWheelPosition3,
            )
        }

//        fun showEwmCode(
//            mContext: Context,
//            ewm: List<String>,
//            mTitle: String = "保存二维码",
//            mBottomText: String = "保存",
//            mPositionTListener: OnPositionTListener<String>? = null
//        ) {
//            if (ewm.isEmpty()) {
//                return
//            }
//            val image = ewm.get(0)
//            showEwnDialog(mContext, image, mTitle, mBottomText, mPositionTListener)
//        }

//        fun showEwnDialog(
//            mContext: Context,
//            image: String,
//            mTitle: String = "保存二维码",
//            mBottomText: String = "保存", mPositionTListener: OnPositionTListener<String>? = null
//        ) {
//            val popupView =
//                ShowImagePopupView(
//                    mContext,
//                    mTitle,
//                    mBottomText,
//                    image,
//                    mPositionTListener
//                )
//            XPopup.Builder(mContext)
//                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
//                .asCustom(popupView)
//                .show()
//        }
    }

}